package com.crane.oss.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.SysException;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.school.bean.request.QueryTestCycleReq;
import com.crane.oss.school.bean.request.testcycle.AddTestCycleReq;
import com.crane.oss.school.bean.request.testcycle.UpdateTestCycleReq;
import com.crane.oss.school.bean.resp.TestCycleResp;
import com.crane.oss.school.dao.TestCycleDao;
import com.crane.oss.school.model.TestCycle;
import com.crane.oss.school.service.TestCycleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author zhenxing.pi
 * @email ccsu123456@qq.com
 * @date 2022年03月26日 14时24分54秒
 */
@Service
@Slf4j
public class TestCycleServiceImpl implements TestCycleService {

    @Resource
    protected TestCycleDao testCycleDao;

    @Override
    public TestCycleResp queryTestCycle(Long id) {
        log.info("TestCycleBOImpl queryTestCycle id={}", id);
        TestCycle testCycle = testCycleDao.selectById(id);
        if (null == testCycle) {
            throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "testCycle");
        }
        TestCycleResp testCycleResp = new TestCycleResp();
        BeanUtil.copyProperties(testCycle, testCycleResp);
        return testCycleResp;
    }

    @Override
    public IPage<TestCycleResp> queryList(QueryTestCycleReq queryRequest) {
        log.info("TestCycleBOImpl queryList queryRequest={}", queryRequest);
        IPage<TestCycleResp> result = new Page<>();
        result.setCurrent(queryRequest.getPageNo());
        result.setPages(queryRequest.getPageSize());

        LambdaQueryWrapper<TestCycle> queryTestCycleWrapper = new LambdaQueryWrapper<>();
        IPage<TestCycle> page = testCycleDao.selectPage(new Page<>(queryRequest.getPageNo(), queryRequest.getPageSize()), queryTestCycleWrapper);
        return page.convert(u -> {
            TestCycleResp v = new TestCycleResp();
            BeanUtils.copyProperties(u, v);
            return v;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(AddTestCycleReq addRequest) {
        log.info("TestCycleBOImpl save request={}", addRequest);
        TestCycle testCycle = new TestCycle();
        BeanUtil.copyProperties(addRequest, testCycle);
        int i = 0;
        try {
            i = testCycleDao.insert(testCycle);
        } catch (Exception e) {
            log.info("TestCycleBOImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(UpdateTestCycleReq updateRequest) {
        log.info("TestCycleServiceImpl update request={}", updateRequest);
        TestCycle testCycle = new TestCycle();
        BeanUtil.copyProperties(updateRequest, testCycle);
        int i = 0;
        try {
            i = testCycleDao.updateById(testCycle);
        } catch (Exception e) {
            log.info("TestCycleServiceImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id) {
        log.info("TestCycleServiceImpl delete id={}", id);
        return testCycleDao.deleteById(id);
    }

}
